#include "textdialog.h"
#include "textlabel.h"
#include "organizedtextcontext.h"

TextDialog::TextDialog(TextLabel * textLabel):
        _textLabel(textLabel),
        _currChar(0),
        _currPage(0),
        _stoped(false),
        _currFlag(TextDialog::FLAG_NORMAL)
{
    _textContext = dynamic_cast<OrganizedTextContext *>(_textLabel->getContext());
    _text = _textLabel->getText();
}

int TextDialog::next(){
    if(_stoped || _currFlag & FLAG_END_OF_TEXT )
        return _currFlag;

    if( _currFlag & FLAG_END_OF_PAGE )
        ++_currPage;

    int flags = FLAG_NORMAL;
    if( TextDialog::isInArray(_currChar, _textContext->getLines()) )
        flags |= FLAG_END_OF_LINE;
    if( TextDialog::isInArray(_currChar, _textContext->getPages()) )
        flags |= FLAG_END_OF_PAGE;
    if( TextDialog::isInArray(_currChar, _textContext->getParagraphs()) )
        flags |= FLAG_END_OF_PARAGRAPH;
    if( _currChar >= GameSystem::getStrLen(_textLabel->getText()) - 1 )
        flags |= FLAG_END_OF_TEXT;

    if( flags & FLAG_END_OF_PAGE || flags & FLAG_END_OF_PARAGRAPH || flags & FLAG_END_OF_TEXT){
        _stoped = true;
    }
    ++_currChar;
    _currFlag = flags;
    return flags;
}

bool TextDialog::isInArray(int value, std::vector<int> * array){
    for(std::vector<int>::iterator it = array->begin(); it != array->end(); ++it)
        if( value == *it )
            return true;
    return false;
}
